﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Data.SqlClient;

namespace Project_ScansiaPacific
{
    public partial class Fr_Transport : DevComponents.DotNetBar.Office2007Form
    {
        public Fr_Transport()
        {
            InitializeComponent();
        }
        private string load_data,MaCT1,SoBB1,SoKHDP,SoKHCT,SLNhap1,UserNhap1,GhiChu1,flag1,Ngay1,UserXuat1,Serial1;
        private int i,j,datalength1,check1;
        private double SLNhapKHDP,SLNhapKHCT,SLMin;
        public void Transport_reserve()
        {
            Process prs = new Process();
            SqlConnection Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            DataSet ds = new DataSet();
            Conn.Open();
            cmd.Connection = Conn;
            GhiChu1 = @"select count(*) from dbo.KV1_KTG";
            try
            {

                cmd.CommandText = GhiChu1;
                lb_count.Text = cmd.ExecuteScalar().ToString();
                load_data = @"select * from KTG_Transport";
                ds = prs.dts(load_data);
                datalength1 = ds.Tables[0].Rows.Count;

                for (i = datalength1 - 1; i > 0; i--)
                {
                    MaCT1 = ds.Tables[0].Rows[i]["MaChiTiet"].ToString();
                    for (j = i - 1; j >= 0; j--)
                    {
                        if (MaCT1 == ds.Tables[0].Rows[j]["MaChiTiet"].ToString())
                        {

                            ds.Tables[0].Rows.RemoveAt(i);
                            //ds.Tables[0].Rows[i].
                            break;
                        }
                    }
                    //dataGridViewX1.Rows.Add(true,ds.Tables[0].Rows[i]["TenSP"].ToString(), ds.Tables[0].Rows[i]["MaChiTiet"].ToString(), ds.Tables[0].Rows[i]["TenChiTiet"].ToString(), ds.Tables[0].Rows[i]["KHDP"].ToString(), ds.Tables[0].Rows[i]["SLKHDP"].ToString(), ds.Tables[0].Rows[i]["SoKH"].ToString(), ds.Tables[0].Rows[i]["SLKH"].ToString(), ds.Tables[0].Rows[i]["SLNhap"].ToString());
                }
                dataGridViewX1.DataSource = ds.Tables[0];
                prs.formatcolordatagrid(dataGridViewX1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }

        }

        private void Fr_Transport_Load(object sender, EventArgs e)
        {
            Transport_reserve();
            dataGridViewX1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridViewX1.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridViewX1.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridViewX1.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridViewX1.Columns[5].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridViewX1.Columns[6].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridViewX1.Columns[7].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridViewX1.Columns[8].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridViewX1.Columns[9].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
        }

        private void bt_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void check_all_CheckedChanged(object sender, EventArgs e)
        {
            if (check_all.Checked == true)
            {
                for (i = 0; i < dataGridViewX1.Rows.Count-1; i++)
                {
                    DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridViewX1.Rows[i].Cells[0];
                    chk.Value = chk.TrueValue;
                }
            }
            else
            {
                for (i = 0; i < dataGridViewX1.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridViewX1.Rows[i].Cells[0];
                    chk.Value=chk.FalseValue;
                }
            }
        }
        private double min_SL(double i, double j)
        {
            if (i <= j)
                return i;
            else
                return j;
        }
        private void Transported()
        {
             Process prs = new Process();
            SqlConnection Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            DataSet ds = new DataSet();
            Conn.Open();
            cmd.Connection = Conn;
            GhiChu1 = @"select count(*) from dbo.KV1_KTG";
            

                
            try
            {
                cmd.CommandText = GhiChu1;
                i = int.Parse(cmd.ExecuteScalar().ToString());
                if (i != int.Parse(lb_count.Text))
                    MessageBox.Show("Dữ liệu đã chỉnh sửa sau khi bạn load dữ liệu, vui lòng đóng form và thực hiện lại");
                else
                {
                   
                    for (i = dataGridViewX1.Rows.Count - 2; i > 0; i--)
                    {
                        MaCT1 = dataGridViewX1.Rows[i].Cells["MaCT"].Value.ToString();
                        for (j = i - 1; j >= 0; j--)
                        {
                            if (MaCT1 == dataGridViewX1.Rows[j].Cells["MaCT"].Value.ToString())
                            {
                                dataGridViewX1.Rows.RemoveAt(i);
                                break;
                            }
                        }
                    }
                    //MessageBox.Show("Không có chú nào bị trùng");
                    SoBB1 = "";
                    flag1 = "1";
                    GhiChu1 = "Chuyển từ DP";
                    UserNhap1 = lb_user.Text;
                    UserXuat1 = "";
                    cmd.CommandText = "Tran_Ins_Up";
                    cmd.CommandType = CommandType.StoredProcedure;
                    check1 = -1;
                    for (i = dataGridViewX1.Rows.Count - 2; i >= 0; i--)
                    {

                        //MessageBox.Show(dataGridViewX1.Rows[i].Cells["MaCT"].Value.ToString());
                        DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridViewX1.Rows[i].Cells[0];

                        if (chk.Value == chk.TrueValue)
                        {
                            SLNhapKHDP = double.Parse(dataGridViewX1.Rows[i].Cells["SLDP"].Value.ToString());
                            SLNhapKHCT = double.Parse(dataGridViewX1.Rows[i].Cells["SLNhap"].Value.ToString());
                            SLMin = min_SL(SLNhapKHDP, SLNhapKHCT);
                            MaCT1 = dataGridViewX1.Rows[i].Cells["MaCT"].Value.ToString();
                            SoKHDP = dataGridViewX1.Rows[i].Cells["KHDP"].Value.ToString();
                            SoKHCT = dataGridViewX1.Rows[i].Cells["SoKH"].Value.ToString();
                            Ngay1 = dataGridViewX1.Rows[i].Cells["Ngay"].Value.ToString();
                            cmd.Parameters.AddWithValue("@MaCT", MaCT1);
                            cmd.Parameters.AddWithValue("@SoBB", SoBB1);
                            cmd.Parameters.AddWithValue("@SLXuat", "0");
                            cmd.Parameters.AddWithValue("@Ngay", Ngay1);
                            cmd.Parameters.AddWithValue("@UserNhap", UserNhap1);
                            cmd.Parameters.AddWithValue("@UserXuat", UserXuat1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                            cmd.Parameters.AddWithValue("@flag", flag1);
                            cmd.Parameters.AddWithValue("@SoKH", SoKHCT);
                            cmd.Parameters.AddWithValue("@SLNhap", SLMin);
                            cmd.Parameters.AddWithValue("@SLTheoDoi", SLMin);
                            cmd.Parameters.AddWithValue("@ID", SoKHCT);
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                            check1 = 1;
                            //MessageBox.Show("Xong 1 chú");
                            SLMin = -SLMin;
                            cmd.Parameters.AddWithValue("@MaCT", MaCT1);
                            cmd.Parameters.AddWithValue("@SoBB", SoBB1);
                            cmd.Parameters.AddWithValue("@SLXuat", "0");
                            cmd.Parameters.AddWithValue("@Ngay", Ngay1);
                            cmd.Parameters.AddWithValue("@UserNhap", UserNhap1);
                            cmd.Parameters.AddWithValue("@UserXuat", UserXuat1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                            cmd.Parameters.AddWithValue("@flag", flag1);
                            cmd.Parameters.AddWithValue("@SoKH", SoKHDP);
                            cmd.Parameters.AddWithValue("@SLNhap", SLMin);
                            cmd.Parameters.AddWithValue("@SLTheoDoi", SLMin);
                            cmd.Parameters.AddWithValue("@ID", SoKHDP);
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                            check1 = 0;
                            dataGridViewX1.Rows.RemoveAt(i);
                            //MessageBox.Show("Xong 2 chú");
                        }
                        
                    }
                    if(check1==0)
                        MessageBox.Show("Dữ liệu được lưu thành công");
                }
                
            }
            catch (Exception ex)
            {
                if (check1 == 1)
                {
                    DataSet ds1 = new DataSet();
                    ds1 = prs.dts("select Serial,SoKH,SLNhap from dbo.KV1_KTG where Serial in (Select max(serial) from dbo.KV1_KTG where SLNhap<>0)");
                    Serial1 = ds1.Tables[0].Rows[0]["Serial"].ToString();
                    SLNhap1 = ds1.Tables[0].Rows[0]["SLNhap"].ToString();
                    SoBB1 = ds1.Tables[0].Rows[0]["SoKH"].ToString();
                    cmd.Parameters.Clear();
                    cmd.CommandText = "Tran_Del_Up";
                    cmd.Parameters.AddWithValue("@Serial", Serial1);
                    cmd.Parameters.AddWithValue("@ID", SoBB1);
                    cmd.Parameters.AddWithValue("@SLTheoDoi", SLNhap1);
                    cmd.ExecuteNonQuery();
                    ds1.Clear();
                }
                MessageBox.Show("Đã xảy ra lỗi khi lưu dữ liệu. /n Vui lòng kiểm tra lại thông tin");
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
                ds.Clear();
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            Transported();
        }
        public void Users(string User)
        {
            lb_user.Text = User;
        }
    }
}